//
// Created by cy on 22-5-4.
//
#include <csignal>
#include <chrono>
#include <thread>
#include "camera.hpp"

using namespace std;

static volatile int SIGNAL_INT = 0;

void signal_handler(int sig) {
    if (sig == SIGINT) {
        SIGNAL_INT = 1;
    }
}

int main(){

    signal(SIGINT, signal_handler);
    auto mainLogger = spdlog::stdout_color_st("MainOut");
    mainLogger->set_level(spdlog::level::info);
    mainLogger->flush_on(spdlog::level::info);

    Camera* cam = Camera::getInstance("../configs/camera.Config");
    if(cam->open())
    {
        cam->setExposureTime(7.5);
        cam->setRoi(std::make_tuple(320, 256), std::make_tuple(640, 512));
        cam->setContrast(300);
        cam->play();

        cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR);

        cv::Mat frame;
        bool ret = false;
        int times = 0;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        while (!SIGNAL_INT) {
            std::tie(ret, frame, times) = cam->read(1000);
            if (ret) {
                cv::imshow("Test Camera", frame);
                cv::waitKey(1);
            } else {
                mainLogger->warn("超时等待1s!!");
            }
        }
        cv::destroyAllWindows();
        cam->close();
    }
    return 0;
}